package org.hit.burkun.umls;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;

public class OmimUmlsMapper extends BaseMapper {
	// http://www.nlm.nih.gov/research/umls/sourcereleasedocs/current/OMIM/
	String otherCondition = "SAB = 'OMIM'";
	private MapidsUmlsDump mapDbh;
	private Entity.TYPE type = Entity.TYPE.OMIM;
	private Set<String> mapped = new HashSet<>();
	private Set<String> allids = new HashSet<>();

	public OmimUmlsMapper(DBHelper dbh, String tbName, MapidsUmlsDump mapDbh) {
		super(dbh, tbName);
		this.mapDbh = mapDbh;
	}

	public OmimUmlsMapper(DBHelper dbh, MapidsUmlsDump mapDbh) {
		this(dbh, "MRCONSO", mapDbh);
	}

	public void quickMap(Collection<String> ids) {
		map(ids, "CUI", "CODE", otherCondition);
	}

	@Override
	public void process(Map<String, Set<String>> data) {
		mapped.addAll(data.keySet());
		mapDbh.saveMappedToDb(type, data);
	}

	@Override
	protected void map(java.util.Collection<String> ids, String conceptFiled,
			String codeFiled, String otherCondition) {
		allids.addAll(ids);
		super.map(ids, conceptFiled, codeFiled, otherCondition);

	};

	@Override
	public void end() {
		allids.removeAll(mapped);
		mapDbh.saveUnMappedToDb(type, allids);
		super.end();
	}

	public Collection<String> getAllOmimData(DBHelper dbh) {
		dbh.connectionDB();
		String queryStr = "select mim_number from omim_mim2gene";
		Collection<String> res = dbh.queryData(queryStr);
		Set<String> hashSet = new HashSet<>(res);
		dbh.disconnection();
		return hashSet;
	}

	public Collection<String> getAllOmimData() {
		return getAllOmimData(this.dbh);
	}

	public static void main(String[] args) {
//		DBHelper dbh = DBHelper.getUMLSDBHelper();
//		MapidsUmlsDump mapDbh = new MapidsUmlsDump(
//				DBHelper.getServerDataDBHelper(), "map_umls_ids");
//		OmimUmlsMapper mapper = new OmimUmlsMapper(dbh, mapDbh);
//		mapper.begin();
//		Collection<String> data = mapper.getAllOmimData(DBHelper
//				.getServerBioSearch());
//		mapper.quickMap(data);
//		mapper.end();
	}

}
